import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;

import java.io.IOException;
import java.util.Scanner;



import com.google.zxing.NotFoundException;
import com.googlecode.javacv.FrameGrabber.Exception;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelChangedListener;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.util.FileManager;


public class WorkBench {

	/**
	 * @param args
	 * @throws java.lang.Exception 
	 * 
	 */
	public static void main(String[] args) throws java.lang.Exception {
		// TODO Auto-generated method stub
		Model m = ModelFactory.createOntologyModel();

		m = FileManager.get().loadModel("smartspace.owl");
		
		m.read(new FileInputStream(new File("smartspace.owl")), "");
		
//		m = FileManager.get().loadModel("smartspace.owl");
//		File infFile = new File("smartspace.owl");
		 //infOntModel.read(infModel.toString());
//		m.read(new FileInputStream(new File("smartspace2.owl")), "");
//		OntModel m = (OntModel) model;
		System.out.println("testQrcode");
		MyContextListener L = new MyContextListener(m);
		m.register(L);
		
		//use sensor to update context
		TestLocationSensor locationSensor = new TestLocationSensor();
		locationSensor.updateModel(m);
		m = L.getOntModel();

		
//     Add the statement to specific model and update it
//       TestAddStatementToModel addStatement = new TestAddStatementToModel();
//		Statement st = addStatement.convertLocationToStatement("Tao", "ClassRoom3", m);
//		m.add(st);
		
//		Statement st2 = addStatement.convertLocationToStatement("Tao", "ClassRoom1", m);
//		addStatement.addStatementToModel(st2, m);
//		m = L.getOntModel();
		
		
		
		//intial query 
/*		TestQuery query = new TestQuery();
        // test input from the eclipse console
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter file's name : ");
        String fileName = null;
        fileName = in.nextLine();        
        System.out.println("You entered : " + fileName);
		
		String queryString = null;
        queryString = query.readQuery(fileName);
		query.runQuery(queryString, m);*/
		
		
		
		
		
		
//		Resource  adam = m.createResource("adam");
//		Resource  judi = m.createResource("judi");
//		Property  parentOf = m.createProperty("test","parentOf");
//		Statement s = m.createStatement(adam, parentOf, judi);
//		m.add(s);
		
		////////////////////////////////

/*		String ex = "http://liris.cnrs.fr/smartspace.owl#";
		OntClass student = m.getOntClass(ex+"Student");
		Individual infIndividual = m.createIndividual(ex+"Tao", student);
		OntClass Restaurant = m.getOntClass(ex+"Restaurant");

		
		Individual ItalyRestaurant = m.createIndividual(ex+"ItalyRestaurant", Restaurant);
		Individual ItalyRestaurant2 = m.createIndividual(ex+"ItalyRestaurant2", Restaurant);
//		Individual classRoom3 = m.getIndividual(ex+"ClassRoom3");
//		System.out.println(frenchRestaurant.toString());
		ObjectProperty hasLocatedIn = m.getObjectProperty(ex+"hasLocatedIn");
		ObjectProperty hasTest = m.createObjectProperty(ex+"hasTest");
		//infIndividual.addProperty(hasLocatedIn,ex+"FranceRestaurant");
		Statement location = m.createStatement(infIndividual, hasLocatedIn, ItalyRestaurant);
		Statement location2 = m.createStatement(infIndividual, hasLocatedIn, ItalyRestaurant2);*/

//		String ex = "http://liris.cnrs.fr/smartspace.owl#";
//		OntClass student = m.getOntClass(ex+"Student");
//		Individual infIndividual = m.createIndividual(ex+"Tao", student);
//		OntClass Restaurant = m.getOntClass(ex+"Restaurant");
//
//		
//		Individual ItalyRestaurant = m.createIndividual(ex+"ItalyRestaurant", Restaurant);
//		Individual ItalyRestaurant2 = m.createIndividual(ex+"ItalyRestaurant2", Restaurant);
////		Individual classRoom3 = m.getIndividual(ex+"ClassRoom3");
////		System.out.println(frenchRestaurant.toString());
//		ObjectProperty hasLocatedIn = m.getObjectProperty(ex+"hasLocatedIn");
//		ObjectProperty hasTest = m.createObjectProperty(ex+"hasTest");
//		//infIndividual.addProperty(hasLocatedIn,ex+"FranceRestaurant");
//		Statement location = m.createStatement(infIndividual, hasLocatedIn, ItalyRestaurant);
//		Statement location2 = m.createStatement(infIndividual, hasLocatedIn, ItalyRestaurant2);

		//////////////////////////////////////////////////////////////////

	
		
//		 TestAddStatementToModel addStatement = new TestAddStatementToModel();
         
//		 addStatement.convertLocationToStatement("Tao", "ChineseRestaurant", m);
//         m.add(location);
//         Statement st2 = addStatement.convertLocationToStatement("Tao", "FrenchRestaurent", m);
//         m = addStatement.AddLocationIndivialToModel("Tao", "TestRestaurant", m);

//		TestAddStatementToModel addStatement = new TestAddStatementToModel();
//		
//		Statement st = addStatement.convertLocationToStatement("Tao", "ChineseRestaurant", m);
//		m.add(st);
//		Statement st2 = addStatement.convertLocationToStatement("Tao", "FrenchRestaurent", m);
//		m = addStatement.addStatementToModel(st2, m);
//		
//		TestReasoner reasoner = new TestReasoner();
//		m = reasoner.runEngine("smartspacehasLocatedIn.rules", m);


//         FileOutputStream fout;


//		TestReasonRule testRule = new TestReasonRule();
//		OntModel infOntModel = testRule.runEngine("smartspacehasLocatedIn.rules", m);
		FileOutputStream fout;
		try {
			fout = new FileOutputStream("testResults5.owl");
			m.write(fout);
			m.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

}
